#include "MenuMaker.h"
#include "Config.h"
#include "Threads.h"
#include "Utils.h"
#include "BrowserBox.h"
/*---------------------------------------------------------------------------*/
MenuMaker::MenuMaker( vector<FileNode*> *vfn, BrowserBox *_flw )
{
	menu = new QMenu("Invisible message");
	
	flw = _flw;
	v_files = *vfn;
	
	v_actions.clear();
	
	// get & pool actions
	int i = 0;
	foreach( FileNode*, pf, v_files )
	{
		//v_actions = poolActions( ( !i ? getActionsFromConfig( *pf ) : v_actions ) , getActionsFromConfig( *pf ) );	
		v_actions = poolActions( ( !i ? getActionsFromConfig( *pf ) : v_actions ) , getActionsFromConfig( *pf ) );
		
		i++;
	}
	
	connectActions();
}
/*---------------------------------------------------------------------------*/
MenuMaker::MenuMaker()
{
	menu = new QMenu("Invisible message");

	
}
/*---------------------------------------------------------------------------*/
MenuMaker::~MenuMaker()
{
	// TODO: do not free memory here !
	
	foreach( MenuAction*, action, v_actions )
	{
		//delete (*action);
	}

	//if(menu) delete menu;
}
/*---------------------------------------------------------------------------*/
void MenuMaker::show()
{
	if(menu)
	{
		menu->popup( QCursor::pos()  );
		menu->exec();	
	}
}
/*---------------------------------------------------------------------------*/
void MenuMaker::runAction(int i)
{
	if(v_actions.size() > i ) v_actions[i]->run();
}
/*---------------------------------------------------------------------------*/
void MenuMaker::setDropMenu(vector<FileNode*> *vfn, BrowserBox *flw)
{
	v_files = *vfn;

	MenuAction *a = new MenuAction( "Copy here" , this ) ;
	a->set( v_files, ACTION_COPYHERE );
	a->setFLW(flw);
	v_actions.push_back(a);
	
	a = new MenuAction( "Move here" , this ) ;
	a->set( v_files, ACTION_MOVEHERE );
	a->setFLW(flw);
	v_actions.push_back(a);
	
	connectActions();
}
/*---------------------------------------------------------------------------*/
vector<MenuAction*> MenuMaker::getActionsFromConfig( FileNode *file )
{
	string what;
	vector<MenuAction*> actions;
	
	if( file->isFolder() )
	{
		what = "$folder";
		addFolderAction( &actions );
	}
	else
	{
		what = file->getExtension();
	}
	
	
	

	
	int count = 0;
	string *str = Config::getValues(what.c_str(), count);
	if( str )
	{
	
		for(int i=0; i<count; i++)
		{		
			QString title = "Open with ";
			title += str[i].c_str();
			
			if( title != "" && title != " " ) 
			{
				MenuAction *action = new MenuAction( title , this ) ;
			
				action->set(
						v_files,
						ACTION_LAUNCH,
						str[i],
						Utils::escapeChars( file->getAbsolutePath() ),
						Utils::escapeChars( file->getParent()->getAbsolutePath() )							
						);
			

				actions.push_back(action);
			}
		}
	}
	
	// Open with
	{
		MenuAction *action = new MenuAction( "Open with..." , this ) ;
			
		action->set(
				v_files,
				ACTION_OPENWITH,
				"$openwith",
				Utils::escapeChars( file->getAbsolutePath() ),
				Utils::escapeChars( file->getParent()->getAbsolutePath() )							
				);
	

		actions.push_back(action);
	}	
		
	actions.push_back( new MenuAction("-", this) );
		
	// Cut Copy Paste delete
	MenuAction *a = new MenuAction( "Cut" , this ) ;
	a->set( v_files, ACTION_CUT );
	actions.push_back(a);
	
	a = new MenuAction( "Copy" , this ) ;
	a->set( v_files, ACTION_COPY );
	actions.push_back(a);
	
	a = new MenuAction( "Paste" , this ) ;
	a->set( v_files, ACTION_PASTE );
	actions.push_back(a);	
		
	a = new MenuAction( "Delete" , this ) ;
	a->set( v_files, ACTION_DELETE );
	actions.push_back(a);	
	
	
	return actions;
}
/*---------------------------------------------------------------------------*/
vector<MenuAction*> MenuMaker::poolActions( vector<MenuAction*> first, vector<MenuAction*> second )
{
	vector<MenuAction*> ret;
	
	//if( !first.size() ) return second;
	
	foreach( MenuAction*, a, first )
	{
		MenuAction *action_a = *a ;
		bool keep = false;
		
		foreach( MenuAction*, b, second )
		{
			MenuAction *action_b = *b ;
			
			if( !action_a->getParam(PARAM_LAUNCHER).compare( action_b->getParam(PARAM_LAUNCHER) ) )
			{
				// pool and keep it
				action_a->appendToParam(PARAM_FILEPATH, action_b->getParam( PARAM_FILEPATH ) );
				
				
				keep = true;
				break;
			}

		}
		
		if( keep )
		{
			ret.push_back(action_a);
		}

	}
	

	return ret;
}
/*---------------------------------------------------------------------------*/
void MenuMaker::addFolderAction( vector<MenuAction*> *v )
{
	MenuAction *act = new MenuAction("Open directory", this);
	act->set( v_files , ACTION_OPENDIR );
	act->setFLW( flw );
	v->push_back( act );
}
/*---------------------------------------------------------------------------*/
void MenuMaker::connectActions()
{
	// create MenuAction
	foreach( MenuAction*, a, v_actions )
	{

			menu->addAction( *a );
	}
		
	connect( menu, SIGNAL( triggered( QAction* ) ), this, SLOT( actionClicked( QAction* ) ) );
}
/*---------------------------------------------------------------------------*/
void MenuMaker::actionClicked( QAction *act )
{
	MenuAction *action = dynamic_cast<MenuAction*>(act);
	
	// same thread
	action->run();
	
	/*
	
	// multi thread
	ActionThread *at = new ActionThread( action );
	at->start();
	
	*/
	
}
/*---------------------------------------------------------------------------*/

